
//  MBHouseDetailVC.m
//  房源、商铺、写字楼详情页
//
//  Created by MB__Lee on 2018/5/24.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBHouseDetailVC.h"

#import "MBNetTool.h"
#import "NSString+CaculateLenght.h"




#import "SDCycleScrollView.h"


@interface MBHouseDetailVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)UIView *tableHeaderView;

//显示右下角的那个圈圈
@property(nonatomic,strong)UILabel *numPerNum; //显示文字
@property(nonatomic,strong)UIView *numBottomView;  // 底部的半透明视图



@property(nonatomic,strong)UIButton *backBtn;//导航栏隐藏时，设置的返回按钮

@end

static NSString *FisrtCellDetail_Identi = @"FisrtCellDetail";
static NSString *SecondDetail_Identi = @"SecondDetail";
static NSString *ThirdDetail_Identi = @"ThirdDetail";
static NSString *FourthDetail_Identi = @"FourthDetail";
static NSString *FifthCell_Identi = @"FifthCell";
static NSString *SixthCell_Identi = @"SixthCell";
static NSString *SeventhDetail_Identi = @"SeventhDetail";
static NSString *EigthCell_Identi = @"EigthCell";
static NSString *NithCell_Idneti = @"NithCell";


@implementation MBHouseDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dealModel = [[DealFangModel alloc] init]; //处理之后的模型
    
    
    //设置导航栏
    [self setNabigatoinBar];
    
    
    
}
-(UIButton *)backBtn{
    if (!_backBtn) {
        UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
        _backBtn = button;
        
        button.frame = CGRectMake(15, 15, 20, 30);
        
        [button setImage:[UIImage imageNamed:@"back_icon_white"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backVCPush:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}
#pragma mark --设置导航栏
-(void)setNabigatoinBar{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backVCPush:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButton;
    
    

}
-(void)backVCPush:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- set 方法进行设置sn
-(void)setSn:(NSString *)sn{
    _sn = sn;
    [self getSourceFromServiceWithSn:sn];
}
-(void)setModel:(MBFangDetailModel *)model{
    _model = model;
    [self getSourceFromServiceWithSn:model.sn];
    
}
-(void)setReModel:(BaseHouseModel *)reModel{
    _reModel = reModel;
    [self getSourceFromServiceWithSn:reModel.sn];
}

#pragma mark --懒加载numPerNum、tableHeaderView、tableview
-(UIView *)numBottomView{
    if (!_numBottomView) {
        UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MBScale(35), MBScale(35))];
        _numBottomView = bottomV;
        bottomV.backgroundColor = MB_mainColor;
        bottomV.alpha = 0.5;
        bottomV.layer.cornerRadius = 0.5 *MBScale(35);
        bottomV.clipsToBounds = YES;
    }
    return  _numBottomView;
}
-(UILabel *)numPerNum{  //  显示 4/5 的文字
    if (!_numPerNum) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MBScale(35), MBScale(35))];
        _numPerNum = label;
        
        label.layer.cornerRadius = 0.5 * MBScale(35);
        label.clipsToBounds = YES;
        //        label.alpha = 0.5;
        label.backgroundColor =  [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        
    }
    return  _numPerNum;
}
-(UIView *)tableHeaderView{
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, MBScale(285))];
        
        SDCycleScrollView *sycle = [SDCycleScrollView cycleScrollViewWithFrame:_tableHeaderView.bounds delegate:self placeholderImage:[UIImage imageNamed:@"cellPlaceHolder"]];
        
        sycle.imageURLStringsGroup = self.model.img;
        sycle.showPageControl = NO;
        CGFloat num_x = sycle.bounds.size.width - MBScale(50);
        CGFloat num_y = sycle.bounds.size.height - MBScale(50);
        CGRect frame = self.numPerNum.frame;
        frame.origin.x = num_x;
        frame.origin.y = num_y;
        
        // 添加底部的 半透明视图
        self.numBottomView.frame = frame;
        [sycle addSubview:self.numBottomView];
        
        [sycle addSubview:self.backBtn];
        
        //显示 滚动的时候的变化文字
        self.numPerNum.frame = frame;
        [sycle addSubview:self.numPerNum];
        self.numPerNum.text = [NSString stringWithFormat:@"1/%zd",self.model.img.count];
        
        
        
        
        [_tableHeaderView addSubview:sycle];
        
    }
    return  _tableHeaderView;
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        
        
        //1--
        [_tableview registerNib:[UINib nibWithNibName:FisrtCellDetail_Identi bundle:nil] forCellReuseIdentifier:FisrtCellDetail_Identi];
        //2--
        [_tableview registerNib:[UINib nibWithNibName:SecondDetail_Identi bundle:nil] forCellReuseIdentifier:SecondDetail_Identi];
        //3--
        [_tableview registerNib:[UINib nibWithNibName:ThirdDetail_Identi bundle:nil] forCellReuseIdentifier:ThirdDetail_Identi];
        //4--
        [_tableview registerNib:[UINib nibWithNibName:FourthDetail_Identi bundle:nil] forCellReuseIdentifier:FourthDetail_Identi];
        //5--
        [_tableview registerNib:[UINib nibWithNibName:FifthCell_Identi bundle:nil] forCellReuseIdentifier:FifthCell_Identi];
        //6--
        [_tableview registerNib:[UINib nibWithNibName:SixthCell_Identi bundle:nil] forCellReuseIdentifier:SixthCell_Identi];
        //7--
        [_tableview registerNib:[UINib nibWithNibName:SeventhDetail_Identi bundle:nil] forCellReuseIdentifier:SeventhDetail_Identi];
        //8--
        [_tableview registerNib:[UINib nibWithNibName:EigthCell_Identi bundle:nil] forCellReuseIdentifier:EigthCell_Identi];
        //9--
        [_tableview registerNib:[UINib nibWithNibName:NithCell_Idneti bundle:nil] forCellReuseIdentifier:NithCell_Idneti];
        
    }
    return  _tableview;
}

-(void)getSourceFromServiceWithSn:(NSString *)sn{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    //    dict[@"sn"] = self.reModel ? self.reModel.sn : @"100048";
    dict[@"sn"] = sn;
    
    [MBNetTool MBPost:Fang_info paramater:dict success:^(id response) {
        
        
        if ([response[@"code"] integerValue] == 10000 ) {
            MBFangDetailModel *model  = [[MBFangDetailModel alloc] initWithDictionary:response[@"data"] error:nil];
            _model = model;
            
            
            _dealModel.model = model;
            
            [self.view addSubview:self.tableview];
            
            self.tableview.tableHeaderView = self.tableHeaderView;
            [self.tableview reloadData];
        }
        
        
        
        
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark --代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  9;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BaseTableCell *cell;
    if (indexPath.row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:FisrtCellDetail_Identi];
    }else if (indexPath.row == 1){
        cell = [tableView dequeueReusableCellWithIdentifier:SecondDetail_Identi];
    }else if (indexPath.row == 2){
        cell  = [tableView dequeueReusableCellWithIdentifier:ThirdDetail_Identi];
        
        __weak typeof(self) weakSelf = self;
        cell.thirdBlock = ^(MBFangDetailModel *model) {
            
            XQDeiltailVC *xq_vc = [[XQDeiltailVC alloc] init];
            xq_vc.model = weakSelf.model;
            
//            [weakSelf.navigationController pushViewController:xq_vc animated:YES];
            [weakSelf presentViewController:xq_vc animated:YES completion:nil];
        };
    }else if (indexPath.row == 3){
        cell  =[tableView dequeueReusableCellWithIdentifier:FourthDetail_Identi];
    }else if (indexPath.row == 4){
        cell = [tableView dequeueReusableCellWithIdentifier:FifthCell_Identi];
    }else if (indexPath.row == 5){
        cell  = [tableView dequeueReusableCellWithIdentifier:SixthCell_Identi];
    }else if (indexPath.row == 6){
        cell = [tableView dequeueReusableCellWithIdentifier:SeventhDetail_Identi];
    }else if (indexPath.row == 7){
        cell = [tableView dequeueReusableCellWithIdentifier:EigthCell_Identi];
    }else if (indexPath.row == 8){
        cell = [tableView dequeueReusableCellWithIdentifier:NithCell_Idneti];
        cell.niBlock = ^(UIViewController *vc) {
            [self presentViewController:vc animated:YES completion:nil];
        };
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _model;
    
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    return  cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    CGFloat height = 0;
    if (indexPath.row == 0) {   //标题
        height  = _dealModel.firstHight;
    }else if (indexPath.row == 1){  // 房租、户型、面积
        height = _dealModel.secondHight;
    }else if (indexPath.row == 2){  // 方式、时间、装修...
        height = _dealModel.thirdHight;
    }else if (indexPath.row == 3){   //发布人
        height = _dealModel.fourthHight;
    }else if (indexPath.row == 4){   //配套设施
        height = _dealModel.fifthHight;
    }else if (indexPath.row == 5){   //
        height = _dealModel.sixHight;
    }else if (indexPath.row == 6){
        height = _dealModel.seventhHight;
    }else if (indexPath.row == 7){
        height = _dealModel.eightHight;
    }else if (indexPath.row == 8){
        height = _dealModel.nithHight;
    }
    return  height;
}

#pragma mark ---SDCycleScrollViewDelegate
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
    NSString *numPerNumString = [NSString stringWithFormat:@"%zd/%zd",index+1,self.model.img.count];
    self.numPerNum.text = numPerNumString;
    self.numPerNum.font = MBFont(14);
}

#pragma mark ---scrollview
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSet_y  = scrollView.contentOffset.y;
    
    if (offSet_y > 150) {
        self.navigationController.navigationBar.hidden  = NO;
        self.backBtn.hidden = YES;
        
    }else{
        self.navigationController.navigationBar.hidden = YES;
        self.backBtn.hidden = NO;
    }
    
}
-(void)viewWillDisappear:(BOOL)animated{
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
